package org.example;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class AsciiTableWriter {

    public static void main(String[] args) {
        String outputPath = "ascii_table.txt"; // 输出文件路径

        try (BufferedWriter writer = new BufferedWriter(new FileWriter(outputPath))) {
            for (int i = 0; i < 128; i++) {
                // 控制字符显示为描述
                String ch;
                if (i < 32 || i == 127) {
                    ch = getControlCharName(i);
                } else {
                    ch = Character.toString((char) i);
                }

                String line = String.format("Dec: %3d   Hex: %02X   Char: %s%n", i, i, ch);
                writer.write(line);
            }
            System.out.println("ASCII 码表已写入: " + outputPath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 控制字符显示名
    private static String getControlCharName(int code) {
        String[] names = {
                "NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK", "BEL",
                "BS", "HT", "LF", "VT", "FF", "CR", "SO", "SI",
                "DLE", "DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB",
                "CAN", "EM", "SUB", "ESC", "FS", "GS", "RS", "US",
                "DEL"
        };
        return (code == 127) ? names[32] : names[code];
    }
}
